package util;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;

import bean.XmlBean;

public class XmlUtil {
	public  static Map<String,XmlBean> strutsxml(String xmlpath) throws Exception{
		 SAXBuilder builder =new SAXBuilder();
		 Document document =builder.build(new File(xmlpath));
		 Element root = document.getRootElement();
		 Map<String,XmlBean>rmap = new HashMap<String, XmlBean>();
		 Element actionroot = root.getChild("action-mapping");
		 List<Element> actio = actionroot.getChildren();
		 for (Element e : actio) {
			XmlBean action =new XmlBean();
			String fname = e.getAttributeValue("name");
			action.setBeanName(fname);
			Element actionform = root.getChild("formbeans");
			List<Element> form  =actionform.getChildren();
			for (Element element : form) {
				if(fname.equals(element.getAttributeValue("name"))){
					String beanclass =element.getAttributeValue("class");
					action.setBeanClass(beanclass);
					
				}
			}
			
			String type = e.getAttributeValue("type");
			String path = e.getAttributeValue("path");
		    action.setMothod(path);
		    action.setActionClass(type);
		    Map<String,String> map = new HashMap<String, String>();
		    
			List<Element> forward = e.getChildren();
			for (Element fo : forward) {
				String vname = fo.getAttributeValue("name");
				String vtype = fo.getAttributeValue("value");
			    map.put(vname, vtype);
			}
			action.setActionForward(map);
			rmap.put(path, action);
		}
		 return rmap;
	  }
}
